/*
 * CropDialog.java
 *
 * Created on November 25, 2008, 10:26 PM
 */

package collage;

import com.jhlabs.image.BoxBlurFilter;
import com.jhlabs.image.CompoundFilter;
import com.jhlabs.image.GrayscaleFilter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JFrame;
import net.java.swingfx.imagecropper.ImageCropper;
import org.jdesktop.application.Action;

/**
 *
 * @author  feriOmari
 */
public class CropDialog extends javax.swing.JDialog {

    /** Creates new form CropDialog */
    
    public CropDialog(java.awt.Frame parent, boolean modal, BufferedImage img) {
        super(parent, modal);
        initComponents();
        

        this.addKeyListener(new KeyListener() {
                public void keyTyped(KeyEvent e) {
                }
                public void keyPressed(KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                        try {
                            CropDialog cropDialog = (CropDialog) e.getSource();
                            cropDialog.done();
                        } catch (Exception ex) {
                            Logger.getLogger(CollageView.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }

                }
                public void keyReleased(KeyEvent e) {
                }
            });

        imageCropper = new ImageCropper(img);
        
        BufferedImageOp filter = new CompoundFilter(new BoxBlurFilter(3, 3, 3), new GrayscaleFilter());
        imageCropper.setFilter(filter);   
        int margin = 20;
        if (img.getWidth() + 2 * margin > cropPanel.getWidth() || img.getHeight() + 2 * margin > cropPanel.getHeight()) {
            imageCropper.setSize(cropPanel.getWidth() - 2 * margin, cropPanel.getHeight() - 2 * margin);
            imageCropper.setLocation(margin, margin);
        } else {
            imageCropper.setSize(img.getWidth(), img.getHeight() );
            imageCropper.setLocation((cropPanel.getWidth() - imageCropper.getWidth()) /2  , (cropPanel.getHeight() - imageCropper.getHeight()) /2 );
        }

        cropPanel.add(imageCropper);
        
        
    }

    public BufferedImage getCroppedImage() {
        return croppedImage;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cropPanel = new javax.swing.JPanel();
        doneButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        setResizable(false);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(collage.CollageApp.class).getContext().getResourceMap(CropDialog.class);
        cropPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("cropPanel.border.title"))); // NOI18N
        cropPanel.setName("cropPanel"); // NOI18N

        javax.swing.GroupLayout cropPanelLayout = new javax.swing.GroupLayout(cropPanel);
        cropPanel.setLayout(cropPanelLayout);
        cropPanelLayout.setHorizontalGroup(
            cropPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 790, Short.MAX_VALUE)
        );
        cropPanelLayout.setVerticalGroup(
            cropPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 459, Short.MAX_VALUE)
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(collage.CollageApp.class).getContext().getActionMap(CropDialog.class, this);
        doneButton.setAction(actionMap.get("done")); // NOI18N
        doneButton.setText(resourceMap.getString("doneButton.text")); // NOI18N
        doneButton.setName("doneButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cropPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(doneButton))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cropPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(doneButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    /*
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                CropDialog dialog = new CropDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    */
    @Action
    public void done() {
        
        croppedImage = imageCropper.getCroppedImage();
        
        setVisible(false);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel cropPanel;
    private javax.swing.JButton doneButton;
    // End of variables declaration//GEN-END:variables

    private BufferedImage croppedImage = null;
    private ImageCropper imageCropper = null;
}
